<template>
  <Header />

  <div class="search-container">
    <el-input class="search1" v-model="search1" placeholder="文章标题搜索" clearable />
    <el-select
      v-model="search2"
      placeholder="请选择分类"
      size="default"
      class="search2"
      multiple
      collapse-tags-tooltip
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.label"
      />
    </el-select>
    <el-button @click="resetArticles" type="primary" class="search-btn">
      <el-icon><Search /></el-icon> 搜索
    </el-button>
  </div>

  <div class="main-container">
    <div class="blog-container" ref="blogContainer">
      <el-card
        v-for="(post, index) in blogPosts"
        :key="post.id"
        class="blog-post"
        :body-style="{ padding: '20px' }"
        @click="articleDetail(post.id)"
        :class="{ 'fade-in': index % 2 === 0, 'slide-up': index % 2 === 1 }"
      >
        <div class="post-content">
          <div class="post-image">
            <el-image :src="post.coverImg" fit="cover" />
          </div>
          <div class="post-details">
            <p class="post-time">
              <img  src="../assets/icon/时间.png" style="width: 16px; height: 16px;vertical-align: middle " alt="发布时间图标" />
              <strong>发布时间：</strong>{{ post.createTime }}
            </p>
            <h2 class="post-title">{{ post.title }}</h2>
            <div class="post-meta">
              <span><el-icon><User /></el-icon><strong>作者：</strong>{{ post.username }}</span>
              <span><el-icon><View /></el-icon><strong>浏览量：</strong>{{ post.viewCount }}</span>
              <!-- 显示点赞数量 -->
              <span>
                <LikeButton :show-button="false"  :targetId="post.id" :likeType="1" />
              </span>
            </div>
            <p class="post-excerpt">{{ truncateText(post.content, 70) }}</p>
            <div class="post-footer">
              <span v-for="(category, index) in post.categoryName" :key="index" class="category">
                <img src="../assets/icon/类别.png" class="category-icon" alt="分类图标" /> {{ category }}
              </span>
              <span class="post-state">{{ post.state }}</span>
            </div>
          </div>
        </div>
      </el-card>
      <div v-if="loading" class="loading">
        <el-icon><Loading /></el-icon> 加载中...
      </div>
      <div v-if="showScrollButton" @click="scrollToTop" class="scroll-to-top">
        <el-icon><ArrowUp /></el-icon>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import Header from "@/components/Header.vue";
import { ArrowUp, Search, Loading } from '@element-plus/icons-vue'
import { ref, onMounted, onBeforeUnmount } from "vue";
import request from "@/api/request";
import router from "@/router";
// 引入 LikeButton 组件
import LikeButton from '@/components/LikeButton.vue';

interface BlogPost {
  id: number | any;
  title: string | any;
  content: string | any;
  coverImg: string | any;
  categoryName: string | any;
  username: string | any;
  viewCount: string | any;
  createTime: string | any;
  state: string | any;
}

const blogPosts = ref<BlogPost[]>([]);
const currentPage = ref(1);
const pageSize = ref(3);
const totalPosts = ref(0);
const search1 = ref('');
const search2 = ref<string[]>([]);
const loading = ref(false);
const showScrollButton = ref(false);
const options = ref<[]>([])


const getArticles = (pageNum: number, pageSize: number) => {
  loading.value = true; // 开始加载
  request.get('/article/articlelist', {
    params: {
      pageNum: pageNum,
      pageSize: pageSize,
      title: search1.value,
      categoryName: search2.value.join(','),
    },
  })
    .then((res: any) => {
      const newPosts = res.data.items.map((post: BlogPost) => {
        post.categoryName = post.categoryName.split(',');
        return post;
      });
      blogPosts.value = [...blogPosts.value, ...newPosts]; // 追加新数据
      totalPosts.value = res.data.total;
      loading.value = false; // 结束加载
    })
    .catch((error: any) => {
      console.error('获取文章失败:', error);
      loading.value = false; // 结束加载
    });
};


const handleScroll = () => {
  const container = document.documentElement || document.body;
  if (container.scrollHeight - container.scrollTop <= window.innerHeight + 50 && !loading.value) {
    currentPage.value++;
    getArticles(currentPage.value, pageSize.value);
  }
  showScrollButton.value = container.scrollTop > 200;
};

const resetArticles = () => {
  blogPosts.value = [];
  currentPage.value = 1;
  getArticles(currentPage.value, pageSize.value);
};

onMounted(async () => {
  await getCategory()
  await getArticles(currentPage.value, pageSize.value);
  window.addEventListener('scroll', handleScroll);
});

onBeforeUnmount(() => {
  window.removeEventListener('scroll', handleScroll);
});

const truncateText = (text: any, maxLength: any) => {
  if (text.length > maxLength) {
    return text.slice(0, maxLength) + '...';
  }
  return text;
};

const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  });
};

const loadingViewCount = ref(false);

const articleDetail = (postId: number) => {
  loadingViewCount.value = true; // 开始加载
  request.get('/article/viewCount', { params: { articleId: postId } })
    .then((res: any) => {
      console.log('浏览量增加成功:', res.data);
      // 跳转到文章详情页面
      router.push({ name: 'ArticleDetail', params: { id: postId } });
    })
    .catch((error: any) => {
      console.error('增加浏览量失败:', error);
    })
    .finally(() => {
      loadingViewCount.value = false; // 结束加载
    });
};
const getCategory = async () => {
  try {
    const response = await request.get('/category/getCategory')
    options.value = response.data.map((item: any) => ({
      value: item.id,
      label: item.categoryName
    }))
  } catch (error) {
    console.error('获取文章类别失败:', error)
  }
}
</script>

<style scoped>
/* 基础样式 */
:root {
  --primary-color: #409EFF;
  --hover-color: #66b1ff;
  --text-color: #333;
  --light-text: #666;
  --border-color: #e6e6e6;
  --bg-color: #f8f8f8;
  --card-bg: #fff;
  --shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 搜索区域 */
.search-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 80px;
  gap: 20px;
  padding: 25px;
  background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  margin-bottom: 40px;
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.search-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
  transform: rotate(30deg);
  z-index: -1;
  animation: shine 4s infinite linear;
}

@keyframes shine {
  0% { transform: translateX(-100%) rotate(30deg); }
  100% { transform: translateX(100%) rotate(30deg); }
}

.search1, .search2 {
  width: 280px;
  border-radius: 12px;
  border: none;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.9);
}

.search1:hover, .search2:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.search1:focus, .search2:focus {
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.3);
}

.search-btn {
  padding: 12px 28px;
  border-radius: 12px;
  background: linear-gradient(to right, #ff8a00, #da1b60);
  border: none;
  color: white;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.search-btn::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(to right,
  rgba(255,255,255,0) 0%,
  rgba(255,255,255,0.3) 50%,
  rgba(255,255,255,0) 100%);
  transform: rotate(30deg);
  transition: all 0.3s ease;
}

.search-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(218, 27, 96, 0.4);
}

.search-btn:hover::after {
  animation: shine 1.5s infinite linear;
}

.search-btn .el-icon {
  margin-right: 8px;
  transition: transform 0.3s ease;
}

.search-btn:hover .el-icon {
  transform: scale(1.1);
}
/* 主容器 - 炫酷版 */
.main-container {
  max-width: 1200px;
  width: 100%;
  margin: -30px auto;
  padding: 0 20px;
  position: relative;
}

.main-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  height: 100%;
  background: radial-gradient(circle at center, rgba(103, 156, 255, 0.05) 0%, transparent 70%);
  z-index: -1;
}

/* 博客容器 - 炫酷版 */
.blog-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 30px;
  padding: 30px 0;
}

/* 博客卡片 - 炫酷版 */
.blog-post {
  border-radius: 16px;
  background-color: var(--card-bg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
  overflow: hidden;
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(5px);
  background: rgba(255, 255, 255, 0.85);
}

.blog-post::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(to right, #6e8efb, #a777e3);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.blog-post:hover {
  transform: translateY(-8px) scale(1.01);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
  background: rgba(255, 255, 255, 0.95);
}

.blog-post:hover::before {
  opacity: 1;
}

/* 卡片内容 - 炫酷版 */
.post-content {
  display: flex;
  gap: 25px;
  position: relative;
}

.post-image {
  width: 280px;
  height: 220px;
  border-radius: 12px;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.4s ease;
  position: relative;
}

.blog-post:hover .post-image {
  transform: scale(1.03);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.post-image::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(110, 142, 251, 0.2) 0%, rgba(167, 119, 227, 0.2) 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.blog-post:hover .post-image::after {
  opacity: 1;
}

.post-details {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  padding-right: 15px;
}

/* 标题 - 炫酷版 */
.post-title {
  margin: 0 0 15px 0;
  font-size: 24px;
  font-weight: 700;
  color: var(--text-color);
  transition: all 0.3s ease;
  position: relative;
  display: inline-block;
}

.post-title::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(to right, #6e8efb, #a777e3);
  transition: width 0.3s ease;
}

.blog-post:hover .post-title {
  color: #5a67d8;
}

.blog-post:hover .post-title::after {
  width: 100%;
}

/* 元信息 - 炫酷版 */
.post-meta {
  display: flex;
  gap: 20px;
  margin-bottom: 15px;
  font-size: 14px;
  color: var(--light-text);
}

.post-meta span {
  display: flex;
  align-items: center;
  transition: color 0.3s ease;
}

.blog-post:hover .post-meta span {
  color: #5a67d8;
}

.post-meta .el-icon {
  margin-right: 5px;
  font-size: 16px;
}

/* 摘要 - 炫酷版 */
.post-excerpt {
  margin: 0 0 20px 0;
  color: var(--light-text);
  line-height: 1.7;
  font-size: 16px;
  position: relative;
  padding-left: 15px;
}

.post-excerpt::before {
  content: '';
  position: absolute;
  left: 0;
  top: 5px;
  bottom: 5px;
  width: 3px;
  background: linear-gradient(to bottom, #6e8efb, #a777e3);
  border-radius: 3px;
}

/* 页脚 - 炫酷版 */
.post-footer {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-top: auto;
}

.category {
  display: inline-flex;
  align-items: center;
  padding: 5px 12px;
  background: linear-gradient(135deg, rgba(110, 142, 251, 0.1) 0%, rgba(167, 119, 227, 0.1) 100%);
  color: var(--primary-color);
  border-radius: 14px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 1px solid rgba(110, 142, 251, 0.3);
}

.blog-post:hover .category {
  background: linear-gradient(135deg, rgba(110, 142, 251, 0.2) 0%, rgba(167, 119, 227, 0.2) 100%);
  transform: translateY(-2px);
}

.category-icon {
  width: 14px;
  margin-right: 6px;
}

.post-state {
  padding: 5px 12px;
  background: rgba(103, 194, 58, 0.1);
  color: #67c23a;
  border-radius: 14px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid rgba(103, 194, 58, 0.3);
}

/* 加载动画 - 炫酷版 */
.loading {
  text-align: center;
  padding: 40px 0;
  color: var(--light-text);
}

.loading .el-icon {
  font-size: 28px;
  margin-bottom: 10px;
  animation: spin 1.5s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 回到顶部按钮 - 炫酷版 */
.scroll-to-top {
  position: fixed;
  bottom: 50px;
  right: 50px;
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 8px 25px rgba(110, 142, 251, 0.4);
  transition: all 0.3s ease;
  z-index: 100;
  border: none;
}

.scroll-to-top:hover {
  transform: translateY(-5px) scale(1.1);
  box-shadow: 0 12px 30px rgba(110, 142, 251, 0.5);
}

.scroll-to-top .el-icon {
  font-size: 24px;
  transition: transform 0.3s ease;
}

.scroll-to-top:hover .el-icon {
  transform: translateY(-3px);
}

/* 动画效果 */
.fade-in {
  animation: fadeIn 0.6s ease forwards;
}

.slide-up {
  animation: slideUp 0.6s ease forwards;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(30px); }
  to { opacity: 1; transform: translateY(0); }
}

</style>